package leetcode

import "fmt"

/*
 * @lc app=leetcode.cn id=228 lang=golang
 *
 * [228] 汇总区间
 */

// @lc code=start
func summaryRanges(nums []int) []string {
	ranges := make([]string, 0, len(nums))
	n := len(nums)
	if n < 1 {
		return ranges
	}
	nums = append(nums, nums[n-1])
	for i, left := 1, nums[0]; i < n+1; i++ {
		if nums[i-1]+1 != nums[i] {
			right := nums[i-1]
			if left == right {
				ranges = append(ranges, fmt.Sprint(left))
			} else {
				ranges = append(ranges, fmt.Sprintf("%d->%d", left, right))
			}
			left = nums[i]
		}
	}
	return ranges
}

// @lc code=end
